﻿<cfsilent>
	<cfscript>
		
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		response = structNew();
		structInsert(response, "success", false, true);
		
		termId = event.getArg("TermID", event.getArg("TID"));
		taskType = event.getArg("TT");
		depId = event.getArg("DepID");
		
		rs_Task = queryNew("tsk_id");
		if ( len(termId) and len(taskType) and len(depId) ) {
			
			transaction {

				sql = "SELECT a.institute_id,
							   a.institute_name 
						FROM t_institute a 
						WHERE a.institute_id = :depId ";
						
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="depId", value=depId, cfsqltype="cf_sql_varchar" );
				
				rs_institute = queryObj.execute( sql=sql ).getResult();
				
				switch (taskType) {
					
					case "P":
						/* 计划课程 */
				
						sql = "SELECT 
								a.tsk_id, a.term_id, a.cid, 'P' taskProp
								FROM t_task a 
									INNER JOIN t_course b ON b.cid = a.cid 
								WHERE 
									a.term_id= :termId 
									AND 
									b.institute_id= :depId 
									AND 
									SUBSTR(a.task_prop, 7, 1) = '0' 
								ORDER BY a.tsk_id";
								
						queryObj = new query( datasource=application.dnsMaster );
						queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
						queryObj.addParam( name="depId", value=depId, cfsqltype="cf_sql_varchar" );
						
						rs_Task = queryObj.execute( sql=sql ).getResult();
						break;
					
					case "C":
						/* 素质选修课 */
				
						sql = "SELECT 
								a.tsk_id, a.term_id, a.cid, 'C' taskProp 
								FROM t_common_task a 
									INNER JOIN t_course b ON b.cid = a.cid 
								WHERE 
									a.term_id= :termId 
									AND 
									b.institute_id= :depId 
									AND 
									SUBSTR(a.task_prop, 7, 1) = '0' 
								ORDER BY a.tsk_id";
						
						queryObj = new query( datasource=application.dnsMaster );
						queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
						queryObj.addParam( name="depId", value=depId, cfsqltype="cf_sql_varchar" );
						
						rs_Task = queryObj.execute( sql=sql ).getResult();
						break;
					
					case "R":
						/* 单开班课程 */
					
						sql = "SELECT 
								a.tsk_id, a.term_id, a.cid, 'R' taskProp 
								FROM t_reopen_task a 
									INNER JOIN t_course b ON b.cid = a.cid 
								WHERE 
									a.term_id= :termId 
									AND 
									b.institute_id= :depId 
									AND 
									SUBSTR(a.task_prop, 7, 1) = '0' 
								ORDER BY a.tsk_id";
						
						queryObj = new query( datasource=application.dnsMaster );
						queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
						queryObj.addParam( name="depId", value=depId, cfsqltype="cf_sql_varchar" );
						
						rs_Task = queryObj.execute( sql=sql ).getResult();
						break;
					
				}
			
				
			
				
				for ( a=1; a LTE rs_Task.recordCount; a++) {
					
					sql = "SELECT 1 
							FROM t_examplan a 
							WHERE a.task_id = :taskId ";
							
					queryObj = new query( datasource=application.dnsMaster );
					queryObj.addParam( name="taskId", value=rs_Task["tsk_id"][a], cfsqltype="cf_sql_varchar" );
					
					rs_taskEx = queryObj.execute( sql=sql ).getResult();
					
					if ( rs_taskEx.recordCount EQ 0 ) {
					
						term = rs_Task["term_id"][a];
						course = rs_Task["cid"][a];
					
						sql = "INSERT INTO t_examplan (
									   task_id,
									   task_type,
									   exidx,
									   is_arrange,
									   term_id,
									   cid
								) VALUES (
								  		:TaskID,
										:TaskType,
										:ExamIndex,
										:IsArrange,
										:Term,
										:Course
								)";
								
						queryObj = new query( datasource=application.dnsMaster );
						queryObj.addParam( name="TaskID", value=rs_Task["tsk_id"][a], cfsqltype="cf_sql_varchar" );
						queryObj.addParam( name="TaskType", value=rs_Task["taskProp"][a], cfsqltype="cf_sql_char" );
						queryObj.addParam( name="ExamIndex", value=rs_Task["taskProp"][a] & "00000", cfsqltype="cf_sql_char" );
						queryObj.addParam( name="IsArrange", value="0", cfsqltype="cf_sql_char" );
						queryObj.addParam( name="Term", value=term, cfsqltype="cf_sql_char" );
						queryObj.addParam( name="Course", value=course, cfsqltype="cf_sql_varchar" );
						
						queryObj.execute( sql=sql );
					
					}
					
				}
				
				tarInstitute = structNew();
				
				structInsert(tarInstitute, "InstituteName", rs_institute.institute_name, true);
				structInsert(tarInstitute, "InstituteID", rs_institute.institute_id, true);
				
				structInsert(response, "institute", tarInstitute, true);
				structInsert(response, "taskType", taskType, true);

				structInsert(response, "success", true, true);

			}
		}
		
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>